#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: caijj
@version: V1.0
@file: test.py
@time: 2022/03/01
"""
from common.globalvar import GlobalVar
from tools import timeutil
from tools.DingTalk_Base import DingTalk_Disaster
from tools.database_tool import DataBaseTool
from tools.ip_tool import IpTool


def update_flag():
    def decorator(func):
        def wrapper(*arg, **kwargs):
            """更新flag值"""
            db = DataBaseTool()
            time = timeutil.dt_strptime("%Y-%m-%d %H%M%S")
            tbname = arg[1]
            target_table_name = 'mega_syn_hand'
            select_sql = "select tbname from {} where tbname='{}'".format(target_table_name, tbname)
            select_result = db.select_sql(select_sql)
            if len(select_result) == 0:
                db = DataBaseTool()
                sql = "insert into {}(tbname, flag, optime) values(%s,%s,%s)".format(target_table_name)
                db.update_sql(sql, (tbname, 0, time))
            else:
                db = DataBaseTool()
                sql = "update {} set flag = 0, optime = '{}' where tbname = '{}'".format('mega_syn_hand', time, tbname)
                db.update_sql(sql)
            func(*arg, **kwargs)
            db = DataBaseTool()
            sql = "update {} set flag = 1 where tbname = '{}'".format('mega_syn_hand', tbname)
            db.update_sql(sql)
        return wrapper
    return decorator


def send_ding_msg():
    def decorator(func):
        def wrapper(*arg, **kwargs):
            """发送钉钉消息"""
            func(*arg, **kwargs)
            ding = DingTalk_Disaster()
            end_time = timeutil.dt_strptime("%Y-%m-%d %H%M%S")
            ding.send_msg('{city}新房户型数据已{time}准备完成，请入库'.format(city=GlobalVar.now_city,time=str(end_time)))
        return wrapper
    return decorator


def get_ip():
    def decorator(func):
        def wrapper(*arg, **kwargs):
            """获取ip列表"""
            ip_list = IpTool().get_ip()
            GlobalVar.ip_list = ip_list
            func(*arg, **kwargs)
        return wrapper
    return decorator
